/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.leeloo.databinding.ui.event;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.jdt.debug.core.IJavaBreakpoint;
import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
import org.eclipse.jdt.debug.core.IJavaThread;

/**
 * Represents the debugger suspending. 
 * 
 * @author Brad Reynolds
 */
public class SuspendEvent extends AbstractEvent {
    private IJavaBreakpoint breakpoint;
    private String display;
    
    public SuspendEvent(DebugEvent[] events) {
        if (events == null) {
            throw new IllegalArgumentException("Parameter events was null."); //$NON-NLS-1$
        }
        
        DebugEvent event = (events.length > 0) ? events[0] : null;
        if (event.getSource() instanceof IJavaThread) {
            IJavaThread thread = (IJavaThread) event.getSource();
            
            //will be the breakpoints that suspended the VM.
            IBreakpoint[] breakpoints = thread.getBreakpoints();
            if (breakpoints != null && breakpoints.length > 0) {
                breakpoint = (IJavaBreakpoint) breakpoints[0];
            } else {
                assert false : "more than one breakpoint was returned"; //$NON-NLS-1$
            }
        }
        
    }   
    
    public String getTypeDisplay() {
        return Messages.getString("SuspendEvent.suspend"); //$NON-NLS-1$
    }
    
    public String getDisplay() {
        if (display == null) {
            if (breakpoint != null) {
                StringBuffer buffer = new StringBuffer();
                try {
                    buffer.append("Type Name [").append(breakpoint.getTypeName()).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
                    if (breakpoint instanceof IJavaLineBreakpoint) {
                        IJavaLineBreakpoint lineBreakpoint = (IJavaLineBreakpoint) breakpoint;
                        buffer.append(", line number [").append(lineBreakpoint.getLineNumber()).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
                    }                    
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                } finally {
                    display = buffer.toString();
                }
            } else {
                display = ""; //$NON-NLS-1$
            }
        }
        
        return display;
    }
}